var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({

  data: {
    imgUrl: globalData.imgUrl,
    activityList:[],
    registerFlag:0,// 0不展示授权信息 1展示
    phoneFlag:0,//0不展示手机授权  1展示
    unionId:'',
    user:[],
    carList:[],
    article:[],

  },
  loadData: function () {
    var that = this;
    this.car();
    this.article();
  },
 
  car: function () {
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    wx.showLoading({title: '请稍等...',})
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/car/indexList",
      dataType: "json",
      method: "GET",
      data: '',
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("car：" + JSON.stringify(res));
        if (res.code == 0) {
          that.setData({carList:res.data})
        }
      }
    })
  },
  
  article: function () {
    var params = new Object();
    params.pageNum = 1;
    params.pageSize = 10;
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/community/hotArticle",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("article：" + JSON.stringify(res));
        if (res.code == 0) {
          that.setData({ article: res.data })
        }
      }
    })
  },
  carList:function(){
    wx.navigateTo({
      url: '../../car/carList/carList',
    })
  },
  carInfo: function (e) {
    let id = e.currentTarget.id;
    wx.setStorageSync("aidKey", id);
    wx.navigateTo({
      url: '../../car/monitor/monitor',
      // url: '../../car/carInfo/carInfo',
    })
  },
  insertCar:function(){
    let user = this.data.user;
    if (commonUtil.isEmpty(user.idNo)) {
      wx.showModal({
        title: '提示',
        content: '请先前往‘我的个人设置’完成身份证信息提交！',
      })
      return;
    }
    wx.navigateTo({
      url: '../../car/insert/insert',
    })
  },
  articleDetail:function(e){
    let id = e.currentTarget.id;
    wx.setStorageSync("articleIdKey", id);
    wx.navigateTo({
      url: '../../community/articleDetail/articleDetail',
    })
  },
  peccancySelect:function(){
    wx.navigateTo({
      url: '../peccancySelect/peccancySelect',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOpenId();
    // this.loadData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.searchUser();
    this.loadData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //用户信息检验,若不存在，则授权,若已存在判断手机号
  getOpenId:function(){
    let that = this;
    wx.login({
      success: function (res) {
        wx.showLoading({title: '请稍等...',})
        wx.request({
          header: { "Cookie": getApp().getJSESSIONID(), "content-Type": "application/x-www-form-urlencoded" },
          url: httpsUrl + "/common/getOpenId",
          dataType: "json",
          method: "GET",
          data: { "code": res.code },
          success: function (res) {
            wx.hideLoading();
            console.log("获取openid返回:" + JSON.stringify(res));
            res = res.data;
            if (res.code == 0) {
              let data = JSON.parse(res.data);
              wx.setStorageSync("openidKey", data.openid);
              wx.setStorageSync("sessionKey", data.session_key);
              that.setData({ unionId:data.unionId })
              console.log("openid:" + data.openid);
              that.searchUser();
              that.loadData();
            } else {
              console.log("获取openid失败：" + res.message);
            }
          }
        })
      }
    })
  },
  //查询用户信息
  searchUser:function(){
    let that = this;
    let openId = wx.getStorageSync("openidKey");
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/userCenter/userInfo",
      dataType: "json",
      method: "GET",
      // data: { "openId": data.openid },
      success: function (res) {
        res = res.data;
        console.log("userInfo:" + JSON.stringify(res));
        if (res.code == 0) {
          var user = res.data;
          that.setData({ user: user })
          if (commonUtil.isEmpty(user.tell)) {
            that.setData({ phoneFlag: 1 })
          }
        } else if (res.code == 99) {
          that.setData({ registerFlag: 1 })//{"code":99,"message":"用户未注册!"}
        }
      }
    })
  },
  //授权信息
  getUserInfo: function (e) {
    var that = this;
    //会弹框询问是否授权，拒绝则得到undefined
    console.log("wxinfo:" + JSON.stringify(e.detail.userInfo));
    let info = e.detail.userInfo;
    if (!commonUtil.isEmpty(info)) {
      var params = new Object();
      params.nick = info.nickName;
      params.sex = info.gender;//性别 1 男，2 女，0未知
      params.img = info.avatarUrl;
      params.country = info.country;
      params.provice = info.province;
      params.city = info.city;
      params.openId = wx.getStorageSync("openidKey");
      params.unionId = this.data.unionId;
      console.log("params:" + JSON.stringify(params));
      commonUtil.showLoading("请稍等...");
      wx.request({
        // header: {"content-Type": "application/x-www-form-urlencoded" },
        url: httpsUrl + "/common/regist",
        dataType: "json",
        method: "POST",
        data: params,
        success: function (result) {
          commonUtil.hideLoading();
          console.log("注册返回：" + JSON.stringify(result));
          var res = result.data;
          if (res.code == 0) {
            commonUtil.showSuccessToast("注册成功");
            that.setData({ registerFlag: 0 })
            that.searchUser();
          } else {
            commonUtil.showModal("注册失败！" + res.message);
          }
        }
      })
    }
  },
  //手机号授权获取
  getphonenumber: function (e) {
    this.setData({ phoneFlag: 0 })
    //console.log("encryptedData:" + JSON.stringify(e.detail.encryptedData));
    if (commonUtil.isEmpty(e.detail.encryptedData)) {
      return;
    }
    let that = this;
    var params = new Object();
    params.encryptedData = e.detail.encryptedData;
    params.iv = e.detail.iv;
    // params.code = wx.getStorageSync("code");
    params.sessionKey = wx.getStorageSync("sessionKey");
    console.log("手机解密参数：" + JSON.stringify(params));
    wx.request({
      header: { "Cookie": getApp().getJSESSIONID(), "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/common/xcxUserPhone",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (result) {
        var res = result.data;
        console.log("解密结果：" + JSON.stringify(res));
        if (res.code == 0) {
          let data = JSON.parse(res.data);
          let phone = data.phoneNumber;
          console.log(phone);
          commonUtil.updatePhoneByWechat(phone);
        }
      }
    })
  },
})